package com.vepick.metier.requetes;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.vepick.beans.Classification;
import com.vepick.beans.Routines;

public class Requetes_classification {
	/*
	 * Lister toutes les classifications
	 */
	public static void afficherLesClassifcations(Connection conn)
			throws SQLException {

		List<Classification> lesClassifcations = new ArrayList<>();
		String requete = "SELECT idclassif, idstation, typeclassif,heuredebutclassif,heurefinclassif FROM CLASSIFICATION ORDER BY idclassif ASC ";
		Statement stmt = conn.createStatement();
		ResultSet res = stmt.executeQuery(requete);
		while (res.next()) {
			lesClassifcations.add(new Classification(res.getInt("idclassif"),
					res.getInt("idstation"), res.getString("typeclassif"), res
							.getString("heuredebutclassif"), res
							.getString("heuredebutclassif")));
		}
		res.close();
		for (int j = 0; j < lesClassifcations.size(); j++) {
			System.out.println("Classifcation n° "
					+ lesClassifcations.get(j).getIdClassif()
					+ ", associe a la station "
					+ lesClassifcations.get(j).getIdStation() + ""
					+ ", son type est : "
					+ lesClassifcations.get(j).getTypeClassif() + ""
					+ ", horaire DEBUT : "
					+ lesClassifcations.get(j).getHeureDebutClassif() + ""
					+ ", horaire FIN"
					+ lesClassifcations.get(j).getHeureFinClassif());
		}
		System.out.println("\n");
	}

	/*
	 * Creer une nouvelle classification
	 */

	public static void enregistrerClassification(Connection conn,
			Classification classif) {
		try {
			String requeteAjoutClassif = "INSERT INTO CLASSIFICATION VALUES(idClassif_seq.nextVal,"
					+ classif.getIdStation()
					+ ",'"
					+ classif.getTypeClassif()
					+ "',"
					+ "TO_DATE('"
					+ classif.getHeureDebutClassif()
					+ "','dd-mm-yyyy hh24:mi:ss')"
					+ ",TO_DATE('"
					+ classif.getHeureFinClassif()
					+ "','dd-mm-yyyy hh24:mi:ss'))";

			Statement stmt = conn.createStatement();
			stmt.executeUpdate(requeteAjoutClassif);

			conn.commit();
		} catch (SQLException e) {
			System.out.println(e);
			System.out
					.println("Erreur a la creation de de la classification !\n");
		}

	}

}